package com.synnex.wms.controller;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import com.synnex.wms.pojo.Huodong;
import com.synnex.wms.pojo.User;
import com.synnex.wms.service.HuodongService;
import com.synnex.wms.service.UserService;
import com.synnex.wms.util.PageUtil;

/**
 * HuodongController   活动
 * 
 * */

@Controller
@RequestMapping(value = "/Huodong")
public class HuodongController {

	
	SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	
	@Autowired
	private HuodongService HuodongService;
	@Autowired
	private UserService UserService;

	 @RequestMapping(value = "/page")
	 public void page(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		int pageIndex = 1;//设置初始的当前页，页面显示的都是第一页
        int pageSize = 4;//设置每一页显示几条数据,用于计算总页数，此处设置的值必须与sql语句的limit最后的数值一样
        PageUtil<Huodong> pageUtil = new PageUtil<Huodong>();//初始化工具类
        List<Huodong> list = new ArrayList<Huodong>();
        if (request.getParameter("pageIndex") != null) {
            pageIndex = Integer.parseInt(request.getParameter("pageIndex"));
        }//对页面上的分页标签传的值,进行获取,也是就点击'上一页或者下一页'传过来的pageindex
        pageUtil.setPageIndex(pageIndex);//保存至工具类
        
        String name = request.getParameter("name");
        Huodong Huodong = new Huodong();
        Huodong.setName(name);
        int number = HuodongService.pagecount(Huodong);//调用service层方法计算出总数据量，就是多少条数据.
        pageUtil.setPageNumber(number);//保存至工具类
        pageUtil.setPageSize(pageSize);//保存至工具类
        pageUtil.setPageCount((int) Math.ceil((double) (pageUtil
                .getPageNumber() / pageUtil.getPageSize())) + 1);//计算出总页数,并封装到工具类
        int index = (pageIndex - 1) * pageSize;//计算出每一页从数据库中第几条数据开始取值，也就是limit后面的第一个数字
        Huodong.setFenye(index);
        list = HuodongService.showlist(Huodong);//调用service层的方法，取得数据库中的值
        pageUtil.setList(list);//保存到工具类中的集合
        request.setAttribute("pageUtil", pageUtil);//传递到页面，存入值栈中
		request.getRequestDispatcher("/jsp/Huodong/list.jsp").forward(request,
				response);
	}
	 
	 @RequestMapping(value = "/page2")
	 public void page2(HttpServletRequest request, HttpServletResponse response)
			 throws ServletException, IOException {
		 int pageIndex = 1;//设置初始的当前页，页面显示的都是第一页
		 int pageSize = 4;//设置每一页显示几条数据,用于计算总页数，此处设置的值必须与sql语句的limit最后的数值一样
		 PageUtil<Huodong> pageUtil = new PageUtil<Huodong>();//初始化工具类
		 List<Huodong> list = new ArrayList<Huodong>();
		 if (request.getParameter("pageIndex") != null) {
			 pageIndex = Integer.parseInt(request.getParameter("pageIndex"));
		 }//对页面上的分页标签传的值,进行获取,也是就点击'上一页或者下一页'传过来的pageindex
		 pageUtil.setPageIndex(pageIndex);//保存至工具类
		 
		 String name = request.getParameter("name");
		 Huodong Huodong = new Huodong();
		 Huodong.setName(name);
		 int number = HuodongService.pagecount(Huodong);//调用service层方法计算出总数据量，就是多少条数据.
		 pageUtil.setPageNumber(number);//保存至工具类
		 pageUtil.setPageSize(pageSize);//保存至工具类
		 pageUtil.setPageCount((int) Math.ceil((double) (pageUtil
				 .getPageNumber() / pageUtil.getPageSize())) + 1);//计算出总页数,并封装到工具类
		 int index = (pageIndex - 1) * pageSize;//计算出每一页从数据库中第几条数据开始取值，也就是limit后面的第一个数字
		 Huodong.setFenye(index);
		 list = HuodongService.showlist(Huodong);//调用service层的方法，取得数据库中的值
		 pageUtil.setList(list);//保存到工具类中的集合
		 request.setAttribute("pageUtil", pageUtil);//传递到页面，存入值栈中
		 request.getRequestDispatcher("/jsp/Huodong/list2.jsp").forward(request,
				 response);
	 }
	 
	 
	
	@RequestMapping(value = "/delete/{id}")
	public String delete(@PathVariable Integer id) {
		HuodongService.deleteByPrimaryKey(id);
		return "redirect:/Huodong/page";
	
	}
	@RequestMapping(value = "/insert")
	public String insert(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String name = request.getParameter("name");
		String introduce = request.getParameter("introduce");
		String kssj = request.getParameter("kssj");
		String jssj = request.getParameter("jssj");
		String zzr = request.getParameter("zzr");
		String zzrid = request.getParameter("zzrid");
		
		Date currentTime = new Date();
		String csrq = formatter.format(currentTime);
		Huodong Huodong = new Huodong();
		
		
		Huodong.setName(name);
		Huodong.setEnddate(csrq);
		Huodong.setIntroduce(introduce);
		Huodong.setKssj(kssj);
		Huodong.setJssj(jssj);
		Huodong.setZzr(zzr);
		Huodong.setZzrid(Integer.parseInt(zzrid));
		
		HuodongService.insert(Huodong);
		return "redirect:/Huodong/page";
	}
	
	
	@RequestMapping("/toUpdateLocPage/{id}")
	public void toUpdateLocPage(@PathVariable("id") Integer id,
			HttpServletResponse response, HttpServletRequest request)
			throws ServletException, IOException {
		
		Huodong Huodong = HuodongService.selectByPrimaryKey(id);
		request.setAttribute("Huodong", Huodong);
		request.getRequestDispatcher("/jsp/Huodong/update.jsp").forward(request,
				response);

	}
	
	@RequestMapping("/a/{id}")
	public void a(@PathVariable("id") Integer id,
			HttpServletResponse response, HttpServletRequest request)
			throws ServletException, IOException {
		
		User User = UserService.selectByPrimaryKey(id);
		request.setAttribute("User", User);
		request.getRequestDispatcher("/jsp/Huodong/add.jsp").forward(request,
				response);

	}
	
	@RequestMapping(value = "/update")
	public String update(Huodong Huodong) {
		
		Date currentTime = new Date();
		String csrq = formatter.format(currentTime);
		Huodong.setEnddate(csrq);
		
		HuodongService.updateByPrimaryKeySelective(Huodong);
		return "redirect:/Huodong/page";
	}
	

}
